package com.cn.integration.aspect;

import com.cn.common.annotation.Auth;
import com.cn.common.model.AuthUser;
import com.cn.common.utils.exception.ServiceException;
import com.cn.common.utils.http.ServletUtil;
import com.cn.common.utils.jwt.JwtUtil;
import com.cn.common.utils.text.StringUtil;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class OperationLogAspect {

    @Pointcut(value = "@annotation(com.cn.common.annotation.Auth)")
    public void authASpect(){

    }

    @Around("@annotation(auth)")
    public void around(Auth auth){
        boolean pass = auth.isLogin();
        if(pass){
            HttpServletRequest request = ServletUtil.getRequest();
            String token = JwtUtil.getToken(request);
            if(StringUtil.isEmpty(token)){
                throw new ServiceException("账户未登录");
            }
            AuthUser user = JwtUtil.getUser(request);
            if(user == null){
                throw new ServiceException("账户未登录");
            }
        }
    }

}